package top.jacktgq.web.servlet;
/**
 * @Author CandyWall
 * @Date 2021/6/20--12:33
 * @Description 处理学生信息的servlet
 */

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import top.jacktgq.dao.StudentDao;
import top.jacktgq.pojo.Student;
import top.jacktgq.utils.MapperUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "UpdateStudentServlet", value = "/updateStudent")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HashMap resultMap = new HashMap();
        Student student = new Student();
        student.setStu_id(request.getParameter("stu_id"));
        String name = request.getParameter("name");
        // name = new String(name.getBytes(StandardCharsets.ISO_8859_1), "utf-8");
        student.setName(name);
        student.setEmail(request.getParameter("email"));
        try {
            student.setBirthdate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birthdate")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        student.setAge(Integer.parseInt(request.getParameter("age")));
        student.setSex(Integer.parseInt(request.getParameter("sex")));
        student.setPhone(request.getParameter("phone"));
        StudentDao studentService = new StudentDao();
        boolean ifSucced = true;
        try {
            studentService.editStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("插入数据失败！");
            ifSucced = false;
        }
        resultMap.put("ifSucceed", ifSucced);
        response.getWriter().write(MapperUtils.mapToJson(resultMap));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
